package com.iris.live.services.report.sources;

import com.iris.live.services.data.models.Lsh经销商表Model;
import com.iris.live.services.data.repositories.CRMRepository;
import com.iris.live.services.data.repositories.Lsh经销商表Repository;
import com.iris.live.services.models.PageData;
import com.iris.live.services.report.Cell;
import com.iris.live.services.report.IExcelReportSource;
import com.iris.live.services.report.Row;
import com.iris.live.services.report.Sheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by lusong on 2017/3/7.
 */
@Component("ExportLiveClueSource")
public class ExportLiveClueSource implements IExcelReportSource {

    private Logger logger = LoggerFactory.getLogger(ExportLiveClueSource.class);

    @Autowired
    private Lsh经销商表Repository lsh经销商表Repository;

    @Autowired
    private CRMRepository crmRepository;

    @Override
    public Sheet[] getSheets(Map<String, Object> context) {
        Sheet[] sheets = new Sheet[1];
        sheets[0] = new Sheet("LiVE线索", "LiVE线索", true);
        return sheets;
    }

    @Override
    public List<Row> getSheetRows(Map<String, Object> context, Sheet sheet) {
        String permit = context.get("permit").toString();
        Lsh经销商表Model lsh经销商表Model = lsh经销商表Repository.getLshDistributorByPermit(permit);
        if (lsh经销商表Model == null) {
            return null;
        }
        String createDateStart = context.get("createDateStart") == null ? null : context.get("createDateStart").toString();
        String createDateEnd = context.get("createDateEnd") == null ? null : context.get("createDateEnd").toString();
        String lastFollowDateStart = context.get("lastFollowDateStart") == null ? null : context.get("lastFollowDateStart").toString();
        String lastFollowDateEnd = context.get("lastFollowDateEnd") == null ? null : context.get("lastFollowDateEnd").toString();
        String state = context.get("state") == null ? null : context.get("state").toString();
        String intentCarLevel = context.get("intentCarLevel") == null ? null : context.get("intentCarLevel").toString();
        String intentCarStyle = context.get("intentCarStyle") == null ? null : context.get("intentCarStyle").toString();
        String pageSize = context.get("pageSize") == null ? "0" : context.get("pageSize").toString();
        String pageIndex = context.get("pageIndex") == null ? "0" : context.get("pageIndex").toString();
        List values = getSurveyResult(permit, createDateStart, createDateEnd, lastFollowDateStart, lastFollowDateEnd,
                state, intentCarLevel, intentCarStyle, Integer.parseInt(pageSize), Integer.parseInt(pageIndex));
        List<Row> list = new ArrayList<>();

        list.add(new Row());
        list.add(new Row());
        list.add(new Row());
        Cell[] cells = new Cell[2];
        cells[0] = new Cell("经销商名称：" + lsh经销商表Model.get经销商名(), 1);
        cells[1] = new Cell("经销商代码：" + lsh经销商表Model.getBmbs(), 3);
        list.add(new Row(cells));
        list.add(new Row());
        list.add(new Row());

        for (Object o : values) {
            list.add(new Row(getCells(o)));
        }

        return list;
    }

    private Cell[] getCells(Object o) {
        Map map = (Map) o;
        Cell[] cells = new Cell[52];
        cells[0] = new Cell(map.get("姓名") == null ? "" : map.get("姓名").toString(), 1);
        cells[1] = new Cell(map.get("称谓") == null ? "" : map.get("称谓").toString(), 2);
        cells[2] = new Cell(map.get("联系电话") == null ? "" : map.get("联系电话").toString(), 3);
        cells[3] = new Cell(map.get("客户地址") == null ? "" : map.get("客户地址").toString(), 4);
        cells[4] = new Cell(map.get("状态") == null ? "" : map.get("状态").toString(), 5);
        cells[5] = new Cell(map.get("意向级别") == null ? "" : map.get("意向级别").toString(), 6);
        cells[6] = new Cell(map.get("首次渠道来源") == null ? "" : map.get("首次渠道来源").toString(), 7);
        cells[7] = new Cell(map.get("首次渠道类型") == null ? "" : map.get("首次渠道类型").toString(), 8);
        cells[8] = new Cell(map.get("首次渠道") == null ? "" : map.get("首次渠道").toString(), 9);
        cells[9] = new Cell(map.get("市场活动") == null ? "" : map.get("市场活动").toString(), 10);
        cells[10] = new Cell(map.get("lead状态") == null ? "" : map.get("lead状态").toString(), 11);
        cells[11] = new Cell(map.get("销售状态") == null ? "" : map.get("销售状态").toString(), 12);
        cells[12] = new Cell(map.get("创建日期") == null ? "" : map.get("创建日期").toString(), 13);
        cells[13] = new Cell(map.get("创建人") == null ? "" : map.get("创建人").toString(), 14);
        cells[14] = new Cell(map.get("当前用户") == null ? "" : map.get("当前用户").toString(), 15);
        cells[15] = new Cell(map.get("当前用户部门") == null ? "" : map.get("当前用户部门").toString(), 16);
        cells[16] = new Cell(map.get("当前用户销售组") == null ? "" : map.get("当前用户销售组").toString(), 17);
        cells[17] = new Cell(map.get("原所属销售顾问") == null ? "" : map.get("原所属销售顾问").toString(), 18);
        cells[18] = new Cell(map.get("经销商") == null ? "" : map.get("经销商").toString(), 19);
        cells[19] = new Cell(map.get("上次跟进日期") == null ? "" : map.get("上次跟进日期").toString(), 20);
        cells[20] = new Cell(map.get("上次跟进通话时长") == null ? "" : map.get("上次跟进通话时长").toString(), 21);
        cells[21] = new Cell(map.get("首次进店日期") == null ? "" : map.get("首次进店日期").toString(), 22);
        cells[22] = new Cell(map.get("上次进店日期") == null ? "" : map.get("上次进店日期").toString(), 23);
        cells[23] = new Cell(map.get("销售顾问跟进总次数") == null ? "" : map.get("销售顾问跟进总次数").toString(), 24);
        cells[24] = new Cell(map.get("意向车系") == null ? "" : map.get("意向车系").toString(), 25);
        cells[25] = new Cell(map.get("意向车型") == null ? "" : map.get("意向车型").toString(), 26);
        cells[26] = new Cell(map.get("首次试驾日期") == null ? "" : map.get("首次试驾日期").toString(), 27);
        cells[27] = new Cell(map.get("最近一次试驾日期") == null ? "" : map.get("最近一次试驾日期").toString(), 28);
        cells[28] = new Cell(map.get("最近一次试驾车型") == null ? "" : map.get("最近一次试驾车型").toString(), 29);
        cells[29] = new Cell(map.get("是否报价") == null ? "" : map.get("是否报价").toString(), 30);
        cells[30] = new Cell(map.get("报价日期") == null ? "" : map.get("报价日期").toString(), 31);
        cells[31] = new Cell(map.get("是否订单") == null ? "" : map.get("是否订单").toString(), 32);
        cells[32] = new Cell(map.get("订单客户姓名") == null ? "" : map.get("订单客户姓名").toString(), 33);
        cells[33] = new Cell(map.get("订单日期") == null ? "" : map.get("订单日期").toString(), 34);
        cells[34] = new Cell(map.get("订单车辆VIN码") == null ? "" : map.get("订单车辆VIN码").toString(), 35);
        cells[35] = new Cell(map.get("是否零售") == null ? "" : map.get("是否零售").toString(), 36);
        cells[36] = new Cell(map.get("零售日期") == null ? "" : map.get("零售日期").toString(), 37);
        cells[37] = new Cell(map.get("零售客户姓名") == null ? "" : map.get("零售客户姓名").toString(), 38);
        cells[38] = new Cell(map.get("零售类型") == null ? "" : map.get("零售类型").toString(), 39);
        cells[39] = new Cell(map.get("是否保险") == null ? "" : map.get("是否保险").toString(), 40);
        cells[40] = new Cell(map.get("是否金融") == null ? "" : map.get("是否金融").toString(), 41);
        cells[41] = new Cell(map.get("是否交车") == null ? "" : map.get("是否交车").toString(), 42);
        cells[42] = new Cell(map.get("交车日期") == null ? "" : map.get("交车日期").toString(), 43);
        cells[43] = new Cell(map.get("售后联系人1") == null ? "" : map.get("售后联系人1").toString(), 44);
        cells[44] = new Cell(map.get("售后联系人电话") == null ? "" : map.get("售后联系人电话").toString(), 45);
        cells[45] = new Cell(map.get("售后联系人微信") == null ? "" : map.get("售后联系人微信").toString(), 46);
        cells[46] = new Cell(map.get("售后联系人地址") == null ? "" : map.get("售后联系人地址").toString(), 47);
        cells[47] = new Cell(map.get("保有品牌") == null ? "" : map.get("保有品牌").toString(), 48);
        cells[48] = new Cell(map.get("保有车型") == null ? "" : map.get("保有车型").toString(), 49);
        cells[49] = new Cell(map.get("战败日期") == null ? "" : map.get("战败日期").toString(), 50);
        cells[50] = new Cell(map.get("战败原因") == null ? "" : map.get("战败原因").toString(), 51);
        cells[51] = new Cell(map.get("激活日期") == null ? "" : map.get("激活日期").toString(), 52);
        return cells;
    }

    private List getSurveyResult(String permit, String createDateStart, String createDateEnd, String lastFollowDateStart,
                                 String lastFollowDateEnd, String state, String intentCarLevel, String intentCarStyle, int pageSize, int pageIndex) {
        List list = new ArrayList();
        try {
            PageData pageData = crmRepository.findLiVEClue(permit, createDateStart, createDateEnd, lastFollowDateStart, lastFollowDateEnd, state, intentCarLevel, intentCarStyle, pageSize, pageIndex);
            list = pageData.getData();
        } catch (Exception e) {
            logger.error("LiVE线索导出程序异常", e);
        }
        return list;
    }

}
